package de.smarthome.szenario;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import de.smarthome.MainActivity;
import de.smarthome.R;
import de.smarthome.adapter.ScenarioAdapter;
import de.smarthome.controllable.MasterController;
import de.smarthome.database.DatabaseHandler;

public class ListScenario extends ListActivity {

	public DatabaseHandler dbHandler;
	private static final int NEW_SCENARIO = Menu.FIRST;
	private static final int DELETE_ALL = NEW_SCENARIO + 1;
	private static final int REFRESH = DELETE_ALL + 1;
	String scenarioName;
	Dialog renameDialog;
	Dialog newScenarioDialog;
	MasterController masterC;
	
	ListView lv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_scenario);
		dbHandler = new DatabaseHandler(this);
		masterC = new MasterController(dbHandler);
		lv = getListView();
		lv.setAdapter(new ScenarioAdapter(this, dbHandler.getScenarioName()));
		System.out.println(dbHandler.getScenarioName());
		dbHandler.getAllScenarios();

		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
					scenarioName = ((TextView) arg1.findViewById(R.id.itemScenarioName)).getText().toString();
					pushJsonMessages();
					dbHandler.activateScenario(scenarioName);
					
					MainActivity.getInstance().finish();
					Intent intent = new Intent(ListScenario.this, MainActivity.class);
					startActivity(intent);
					
					ListScenario.this.finish();
			}		
		
		     
		});
		
		lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int arg2, long arg3) {
				scenarioName = ((TextView) view.findViewById(R.id.itemScenarioName)).getText().toString();
				
				

				AlertDialog.Builder dialog = new AlertDialog.Builder(ListScenario.this);
				
				final CharSequence[] items = { "Details", "Rename", "Delete" };
				dialog.setTitle("Options");
				dialog.setItems(items, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(which == 0) {
							Bundle bundle = new Bundle();
							bundle.putString("name", scenarioName);
							
							Intent intent = new Intent(ListScenario.this, DetailScreen.class);
							intent.putExtras(bundle);
							startActivity(intent, bundle);
						}

						if (which == 1) {
							renameDialog = new Dialog(ListScenario.this);
							renameDialog.setContentView(R.layout.dialog_create_scenario);
							renameDialog.setTitle("Rename");
							
							Button dialogButton = (Button) renameDialog.findViewById(R.id.dismissButton);
							// if button is clicked, close the custom dialog
							dialogButton.setOnClickListener(new OnClickListener() {
								
								
								@Override
								public void onClick(View v) {
									EditText name = (EditText)renameDialog.findViewById(R.id.newScenarioName);
									String newName = name.getText().toString();
									if(!(newName.equals(""))){
									dbHandler.changeScenarioName(newName,scenarioName);
									refresh();
									}
									renameDialog.dismiss();
									}
							});
							renameDialog.show();
						}

						
						if (which == 2) {
							dbHandler.deleteScenario(ListScenario.this.scenarioName);
							refresh();
						}
						
						
							
					}
				});

				dialog.show();
				System.out.println("ARGS: " + arg2 + " --- " + arg3);
				System.out.println("VIEW: " + view.findViewById(R.id.itemScenarioName));
				return false;
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(0, NEW_SCENARIO, 0, "create Situation");
		menu.add(0, DELETE_ALL, 0, "delete all");
		menu.add(0, REFRESH, 0, "refesh list");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case NEW_SCENARIO:
			newScenarioDialog = new Dialog(ListScenario.this);
			newScenarioDialog.setContentView(R.layout.dialog_create_scenario);
			newScenarioDialog.setTitle("Create New Situation");
			
			Button dialogButton = (Button) newScenarioDialog.findViewById(R.id.dismissButton);
			// if button is clicked, close the custom dialog
			dialogButton.setOnClickListener(new OnClickListener() {
				
				
				@Override
				public void onClick(View v) {
					EditText name = (EditText)newScenarioDialog.findViewById(R.id.newScenarioName);
					String nameOfScenario = name.getText().toString();
					if(!(nameOfScenario.equals(""))){
		            	if(dbHandler.containsScenarioName(nameOfScenario)){
			            	dbHandler.deleteScenario(nameOfScenario);
		            	}
					dbHandler.saveStatusForScenario(nameOfScenario);
					refresh();
					}
					newScenarioDialog.dismiss();
					}
			});
			newScenarioDialog.show();
			break;

		case DELETE_ALL:
			dbHandler.deleteAllScenario();

			refresh();
			break;

		case REFRESH:
			refresh();
			break;

		default:
			return super.onOptionsItemSelected(item);

		}
		return true;
	}

	public void refresh() {
		lv.setAdapter(new ScenarioAdapter(this, dbHandler.getScenarioName()));
	}
	
	public void pushJsonMessages(){
		List<ScenarioData> scenarioDatas = dbHandler.getScenarioInfo(scenarioName);
		for (ScenarioData elem : scenarioDatas) {
			String room = elem.getRoomName();
			
			if(!(room.equals("kitchen"))){
			if(elem.getBlind()==1){masterC.blindOpen(room);}
			else{masterC.blindClose(room);}
			}
			
			if(elem.getCurtain()==1){masterC.curtainOpen(room);}
			else{masterC.curtainClose(room);}
			
			if(elem.getWindow()==1){masterC.windowOpen(room);}
			else{masterC.windowClose(room);}
			
			if(elem.getLight()==1){masterC.lightOn(room);}
			else{masterC.lightOff(room);}

			masterC.changeColor(room, elem.getColor());
			
		}
	}
}
